package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.entity.ServiceNurseTraining;
import com.web.exiaodao.business.repository.ServiceNurseTrainingRepository;
import com.web.exiaodao.remote.service.ApiServiceNurseTrainingService;
import com.web.exiaodao.remote.vo.ServiceNurseTrainingVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2016-11-03 18:26:49
 * 类的说明：前端APINurseTraining接口实现类
 **/
@Service("ApiNurseTrainingService")
@Transactional(readOnly = false)
public class ApiServiceNurseTrainingServiceImpl extends BaseService<ServiceNurseTraining> implements ApiServiceNurseTrainingService {

	private static final Logger LOGGER = Logger.getLogger(ApiServiceNurseTrainingServiceImpl.class);

	@Autowired
	ServiceNurseTrainingRepository nurseTrainingRepository;
	
	@Override
	public XaResult<ServiceNurseTrainingVo> findOne(Long tId) throws BusinessException {
		ServiceNurseTraining obj = nurseTrainingRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		//保存点击量
        obj.setClickCont(1 + NumberUtils.toLong(obj.getClickCont()+""));
        nurseTrainingRepository.save(obj);
		
		XaResult<ServiceNurseTrainingVo> xr = new XaResult<ServiceNurseTrainingVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),ServiceNurseTrainingVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->ServiceNurseTraining");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<ServiceNurseTrainingVo> findLastOne() throws BusinessException {
		XaResult<ServiceNurseTrainingVo> xr = new XaResult<>();
		Map<String, SearchFilter> filters = new HashMap<>();
			filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
			List<ServiceNurseTraining> entitys = nurseTrainingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceNurseTraining.class), new Sort(
				new Sort.Order[]{
				new Sort.Order(Sort.Direction.DESC, "createTime"),
				new Sort.Order(Sort.Direction.DESC, "id")
				}));
				if (entitys == null || entitys.size() == 0) {
				xr.error("暂无信息");
				} else {
				xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), ServiceNurseTrainingVo.class));
				xr.getObject().setId(entitys.get(0).getId());
				LOGGER.info("查询单个实体----->ServiceNurseTraining");
			}
		return xr;
	}

	@Override
	public XaResult<List<ServiceNurseTrainingVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ServiceNurseTraining> page = nurseTrainingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceNurseTraining.class), pageable);
		XaResult<List<ServiceNurseTrainingVo>> xr = new XaResult<List<ServiceNurseTrainingVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ServiceNurseTrainingVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<ServiceNurseTrainingVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ServiceNurseTraining> page = nurseTrainingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceNurseTraining.class), pageable);
		XaResult<List<ServiceNurseTrainingVo>> xr = new XaResult<List<ServiceNurseTrainingVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ServiceNurseTrainingVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<ServiceNurseTrainingVo> multiOperate(String modelIds,
														 Integer status) throws BusinessException {
		XaResult<ServiceNurseTrainingVo> xr = new XaResult<ServiceNurseTrainingVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ServiceNurseTraining obj = nurseTrainingRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = nurseTrainingRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ServiceNurseTrainingVo.class));
					xr.getObject().setId(obj.getId());
				}
			}
		}
		LOGGER.info("更改数据状态----->ServiceNurseTraining");
		return xr;
	}

	@Override
	public XaResult<ServiceNurseTrainingVo> createModel(ServiceNurseTraining model)
			throws BusinessException {
		XaResult<ServiceNurseTrainingVo> xr = new XaResult<ServiceNurseTrainingVo>();
		ServiceNurseTraining obj = nurseTrainingRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ServiceNurseTrainingVo.class));
		xr.getObject().setId(obj.getId());
		LOGGER.info("新增数据成功----->ServiceNurseTraining");
		return xr;
	}

	@Override
	public XaResult<ServiceNurseTrainingVo> updateModel(ServiceNurseTraining model)
			throws BusinessException {
		ServiceNurseTraining obj = nurseTrainingRepository.findOne(model.getId());
		XaResult<ServiceNurseTrainingVo> xr = new XaResult<ServiceNurseTrainingVo>();
		if (XaUtil.isNotEmpty(obj)) {
					obj.setNurseTraining_Name(XaUtil.isNotEmpty(model.getNurseTraining_Name()) ? model.getNurseTraining_Name() : obj.getNurseTraining_Name());
					obj.setNurseTraining_StartTime(XaUtil.isNotEmpty(model.getNurseTraining_StartTime()) ? model.getNurseTraining_StartTime() : obj.getNurseTraining_StartTime());
					obj.setNurseTraining_EndTime(XaUtil.isNotEmpty(model.getNurseTraining_EndTime()) ? model.getNurseTraining_EndTime() : obj.getNurseTraining_EndTime());
					obj.setNurseTraining_Address(XaUtil.isNotEmpty(model.getNurseTraining_Address()) ? model.getNurseTraining_Address() : obj.getNurseTraining_Address());
					obj.setNurseTraining_organization(XaUtil.isNotEmpty(model.getNurseTraining_organization()) ? model.getNurseTraining_organization() : obj.getNurseTraining_organization());
					obj.setQuantity(XaUtil.isNotEmpty(model.getQuantity()) ? model.getQuantity() : obj.getQuantity());
					obj.setPrice(XaUtil.isNotEmpty(model.getPrice()) ? model.getPrice() : obj.getPrice());
					obj.setQuantityInventory(XaUtil.isNotEmpty(model.getQuantityInventory()) ? model.getQuantityInventory() : obj.getQuantityInventory());
					obj.setOrderStartTime(XaUtil.isNotEmpty(model.getOrderStartTime()) ? model.getOrderStartTime() : obj.getOrderStartTime());
					obj.setOrderEndTime(XaUtil.isNotEmpty(model.getOrderEndTime()) ? model.getOrderEndTime() : obj.getOrderEndTime());
					obj.setNurseTraining_Content(XaUtil.isNotEmpty(model.getNurseTraining_Content()) ? model.getNurseTraining_Content() : obj.getNurseTraining_Content());
					obj.setNurseTraining_url(XaUtil.isNotEmpty(model.getNurseTraining_url()) ? model.getNurseTraining_url() : obj.getNurseTraining_url());
					obj.setMobile(XaUtil.isNotEmpty(model.getMobile()) ? model.getMobile() : obj.getMobile());
				obj = nurseTrainingRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ServiceNurseTrainingVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->ServiceNurseTraining");
		return xr;
	}

}
